<template>
  <div class="url-picture-upload">
    <a-input-group compact>
      <a-input v-model:value="fileUrl" style="width: calc(100% - 120px)" placeholder="请输入图片地址"/>
      <a-button type="primary" style="width: 120px" :loading="loading" @click="handleUpload">上 传</a-button>
    </a-input-group>
    <div class="img-wrapper">
      <img v-if="picture?.url" :src="picture?.url" alt="avatar" />
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { message } from 'ant-design-vue';
import type { UploadProps } from 'ant-design-vue';
import { uploadPictureByUrlUsingPost, uploadPictureUsingPost } from '@/api/pictureController.ts'

interface Props {
  picture?: API.PictureVO;
  spaceId?: number;
  onSuccess?: (newPicture: API.PictureVO) => void;
}

const props = defineProps<Props>();
const fileUrl = ref<string>('');

const loading = ref<boolean>(false);

/**
 * 上传图片
 * @param file
 */
const handleUpload = async () => {
  loading.value = true;
  try {
    const params: API.PictureUploadRequest = {fileUrl : fileUrl.value}
    params.spaceId = props.spaceId;
    if (props.picture){
      params.id = props.picture.id;
    }
    const res = await uploadPictureByUrlUsingPost(params)
    if (res.data.code === 0 && res.data.data) {
      message.success('上传成功');
      // 将上传成功的图片信息传递给父组件
      props.onSuccess?.(res.data.data);
    } else {
      message.error('上传失败' + res.data.message);
    }
  }catch (e) {
    console.error('上传失败' + e);
    message.error('上传失败' + e.message);
  }
  loading.value = false;
}



</script>
<style scoped>
.url-picture-upload {
  margin-bottom: 16px;
}

.url-picture-upload img {
  max-width: 100%;
  max-height: 480px;
}

.url-picture-upload .img-wrapper {
  text-align: center;
  margin-top: 16px;
}
</style>
